#include "ros_imagemsg/image_trans.h"

image_transport_pub::image_transport_pub()
{
    ROS_INFO("generate a new tool");
    // ros::NodeHandle nh_;
    cv::namedWindow("view image_sub");
    cv::startWindowThread();
    ROS_INFO("make_shared it ...");
    it = boost::make_shared<image_transport::ImageTransport>(nh_);
    ROS_INFO("set the topic");
    sub = it->subscribe("/camera/image_raw", 1, &image_transport_pub::imageCallback, this);
    pub = it->advertise("/camera/image_out", 1);
}

image_transport_pub::~image_transport_pub()
{
    cv::destroyWindow("view image_sub");
}

void image_transport_pub::imageCallback(const sensor_msgs::ImageConstPtr &msg)
{
    try
    {
        // ROS_INFO("camera_image",msg->encoding.c_str());
        image_sub = cv_bridge::toCvCopy(msg, "bgr8")->image;
        // ROS_INFO("receive one frame...");
        cv::imshow("view image_sub", image_sub);
        cv::imshow("view image_sub", cv_bridge::toCvCopy(msg, "bgr8")->image);
        cv::waitKey(30);
    }
    catch (cv_bridge::Exception &e)
    {
        ROS_ERROR("Could not convert from '%s' to 'bgr8'.", msg->encoding.c_str());
    }

    image_pub_msgs = cv_bridge::CvImage(std_msgs::Header(), "bgr8", image_sub).toImageMsg();
    pub.publish(msg);
}
